import axios from "axios";
import {
  IHome,
  IGetDdq,
  IGetSale,
  ISale,
  IDdq,
  IFav,
  ISnap,
  IGetFav,
  ICoupon,
  IGetCoupon,
  IGetSnap,
  IBanner,
  IGetBanner,
  IAction,
  IGetAction,
  IFind,
  IGetFind,
  IGood,
  IGetGood,
} from "./interface";
const home: IHome = {
  namespaced: true,
  state: {
    class: [
      {
        id: 1,
        data: "美食",
        path: "/cat?cid=6",
      },
      {
        id: 2,
        data: "居家日用",
        path: "/cat?cid=4",
      },
      {
        id: 3,
        data: "女装",
        path: "/cat?cid=1",
      },
      {
        id: 4,
        data: "美妆",
        path: "/cat?cid=3",
      },
      {
        id: 5,
        data: "数码家电",
        path: "/cat?cid=8",
      },
      {
        id: 6,
        data: "鞋品",
        path: "/cat?cid=5",
      },
      {
        id: 7,
        data: "男装",
        path: "/cat?cid=9",
      },
      {
        id: 8,
        data: "内衣",
        path: "/cat?cid=10",
      },
      {
        id: 9,
        data: "母婴",
        path: "/cat?cid=2",
      },
      {
        id: 10,
        data: "家装家纺",
        path: "/cat?cid=14",
      },
      {
        id: 11,
        data: "文娱车品",
        path: "/cat?cid=7",
      },
      {
        id: 12,
        data: "配饰",
        path: "/cat?cid=12",
      },
      {
        id: 13,
        data: "箱包",
        path: "/cat?cid=11",
      },
      {
        id: 14,
        data: "户外运动",
        path: "/cat?cid=13",
      },
    ],
    banner: [],
    action: [],
    snap: [],
    favLogo: "",
    fav: [],
    ddq: [],
    sale: {
      basic: {
        moduleVisible: "",
      },
      css: {
        layout: 0,
      },
      reports: [],
      newGoodsCountOfToday: 0,
      barandDTOS: [],
    },
    coupon: {
      total_quan_over: {
        now_time: "",
        now_num: 0,
        pre_time: "",
        pre_num: 0,
      },
      choose: {
        obj: {
          title: "",
          px: "",
          pxType: "",
          blackId: "",
        },
      },
      list: [],
    },
    find: [],
    good: [],
  },
  actions: {
    async getsnap({ commit }) {
      const res: ISnap[] = await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=28849&proModelId=2&source=3&version=v1&tuserId=771131&isWechat=0"
      ).then((res): ISnap[] => {
        const result: ISnap[] = JSON.parse(
          JSON.stringify(res.data.data.config.data)
        );
        return result;
      });
      const action: IGetSnap = {
        type: "GETSNAP",
        payload: res,
      };
      commit(action);
    },
    async getbanner({ commit }) {
      const res: IBanner[] = await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=-1&proModelId=1&source=3&version=v1&advVersion=1&tuserId=771131&isWechat=0"
      ).then((res) => {
        const result: IBanner[] = JSON.parse(
          JSON.stringify(res.data.data.config)
        );
        return result;
      });
      const action: IGetBanner = {
        type: "GETBANNER",
        payload: res,
      };
      commit(action);
    },
    async getaction({ commit }) {
      const res: IAction[] = await axios(
        "http://cmsjapi.ffquan.cn/api/category/home/model-detail-by-model-id-new-revision?entityId=4&modelId=28848&proModelId=24&source=3&version=v1&advVersion=1&tuserId=771131&isWechat=0"
      ).then((re) => {
        const result: IAction[] = JSON.parse(
          JSON.stringify(re.data.data.config.oneBanners)
        );
        return result;
      });
      const action: IGetAction = {
        type: "GETACTION",
        payload: res,
      };
      commit(action);
    },
    async getfav({ commit }) {
      let res: IFav[] = [],
        logo = "";
      await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=28850&proModelId=13&source=3&version=v1&tuserId=771131&isWechat=0"
      ).then((re): void => {
        res = re.data.data.config.list;
        logo = re.data.data.config.logo;
      });
      const action: IGetFav = {
        type: "GETFAV",
        payload: { logo, fav: res },
      };
      commit(action);
    },
    async getddq({ commit }) {
      const res: IDdq[] = await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=28853&proModelId=20&source=3&version=v1&v=1623304658129&random=1518&tuserId=771131&isWechat=0"
      ).then((res): IDdq[] => {
        return res.data.data.ddqSessions;
      });
      const action: IGetDdq = {
        type: "GETDDQ",
        payload: res,
      };
      commit(action);
    },
    async getsale({ commit }) {
      const res: ISale = await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=28854&proModelId=21&source=3&version=v1&tuserId=771131&isWechat=0"
      ).then((res): ISale => {
        return res.data.data;
      });
      const action: IGetSale = {
        type: "GETSALE",
        payload: res,
      };
      commit(action);
    },
    async getcoupon({ commit }) {
      const res: ICoupon = await axios(
        "http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new?entityId=4&modelId=28855&proModelId=16&source=3&version=v1&tuserId=771131&isWechat=0"
      ).then((res): ICoupon => {
        return res.data.data.config;
      });
      const action: IGetCoupon = {
        type: "GETCOUPON",
        payload: res,
      };
      commit(action);
    },
    async getfind({ commit }, { page }) {
      const res: IFind[] = await axios(
        `http://cmsjapi.ffquan.cn/api/category/index/lingquan-live-new?pageId=${page}&pageSize=20&entityId=4&type=1&version=v1&advVersion=1&tuserId=771131&isWechat=0`
      ).then((res): IFind[] => {
        return res.data.data.list;
      });
      const action: IGetFind = {
        type: "GETFIND",
        payload: res,
      };
      commit(action);
    },
    async getgood({ commit }) {
      const res: IGood[] = await axios(
        "http://cmsjapi.ffquan.cn/api/goods/guess-you-like?entityId=4&type=1&tuserId=771131&isWechat=0&keyWords=%E7%B2%BD%E5%AD%90"
      ).then((res): IGood[] => {
        return res.data.data;
      });
      const action: IGetGood = {
        type: "GETGOOD",
        payload: res,
      };
      commit(action);
    },
  },
  mutations: {
    GETSNAP(state, action) {
      state.snap = action.payload;
    },
    GETBANNER(state, action) {
      state.banner = action.payload;
    },
    GETACTION(state, action) {
      state.action = action.payload;
    },
    GETFAV(state, action) {
      state.fav = action.payload.fav;
      state.favLogo = action.payload.logo;
    },
    GETDDQ(state, action) {
      state.ddq = action.payload;
    },
    GETSALE(state, action) {
      state.sale = action.payload;
    },
    GETCOUPON(state, action) {
      state.coupon = action.payload;
    },
    GETFIND(state, action) {
      state.find.push(...action.payload);
    },
    GETGOOD(state, action) {
      state.good = action.payload;
    },
  },
};
export default home;
